/*
 * @Author: guoyangf
 * @Date: 2020-07-02 13:39:23
 * @LastEditTime: 2020-08-03 18:12:19
 * @Description: 接单按钮
 */
import React, { useState, useEffect } from 'react';
import { base } from 'nc-lightapp-front';
import { requestPromise } from 'ssccommon/utils/asyncFunctions.js';
import {
    toast,
} from 'nc-lightapp-front';
import './index.less';
const { NCButton } = base

//获取接单状态
async function checkGetBillStatus(data) {
    try {
        const result = await requestPromise({
            url: '/nccloud/ssctp/intellidispatch/QueryNowMyStateAction.do',
            data
        });
        return result
    } catch (e) {
        toast({ title: e.message, duration: 5, color: 'danger', })
    }
}

/**
 *开始接单
 *
 * @param {*} data
 * @returns
 */
async function setStart(data) {
    try {
        const result = await requestPromise({
            url: `/nccloud/ssctp/intellidispatch/SetStartStateAction.do`,
            data
        });
        return result
    } catch (e) {
        toast({ title: e.message, duration: 5, color: 'danger', })
    }
}

/**
 *停止接单
 *
 * @param {*} data
 * @returns
 */
async function setEnd(data) {
    try {
        const result = await requestPromise({
            url: '/nccloud/ssctp/intellidispatch/SetEndStateAction.do',
            data
        });
        return result
    } catch (e) {
        toast({ title: e.message, duration: 5, color: 'danger', })
    }
}

function TakeOrderButtonCtrl(props) {
    
    useEffect(() => {
        checkGetBillStatus().then((result) => {
            setIsTakeOrder(result.data.state);
        });
    }, [])
    const multiLang = props.multiLang;
    const [btnName, setBtnName] = useState(multiLang['job-common-jdz']); /*接单中*/
    const [isTakeOrder, setIsTakeOrder] = useState(false)
    let theme = null;
    let getBillClass = null;
    if (theme === 'black') { // 暗黑主题接单css类
        getBillClass = `get-bill--black`
    } else { // 其他主题接单css类
        getBillClass = `get-bill--default`
    }
    const orderInnerContent = (
        isTakeOrder
            ?
            (
                <span className={'active'} >
                    <i className='iconfont icon-jiedanzhong'></i>
                    <span >{btnName}</span>
                </span>
            )
            :
            (
                <span className={'active'} >
                    <i className='iconfont icon-kaishijiedan'></i>
                    <span >{multiLang['job-common-ksjd']}</span> {/* 开始接单 */}
                </span>
            )
    )

    return (
        <div className="get-bill-checkbox">
            <NCButton
                className={getBillClass}
                onClick={async () => {
                    if (isTakeOrder) {
                        await setEnd();
                    } else {
                        await setStart();
                    }
                    setIsTakeOrder(!isTakeOrder)
                }}
                onMouseEnter={() => setBtnName(multiLang['job-common-tzjd'])} /*停止接单*/
                onMouseLeave={() => setBtnName(multiLang['job-common-jdz'])} /*接单中*/
            >
                {orderInnerContent}
            </NCButton>
        </div>
    );
}


export default TakeOrderButtonCtrl;